package top.lingkang.finalpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;


public final class FinalPoolUtils {
    private static final Logger logger = LoggerFactory.getLogger(FinalPoolUtils.class);

    public FinalPoolUtils() {
        new IllegalAccessError("cannot new object");
    }

    private static FinalPool finalPool = null;
    private static FinalPoolConfig finalPoolConfig;

    public static void init(FinalPoolConfig config) {
        finalPoolConfig = config;
        logger.info("FinalPoolUtils init finish!");
    }



    public static FinalDataSource getDataSource() {
        if (finalPool == null) {
            finalPool = new FinalPool();
            if (finalPoolConfig == null) {
                FinalPoolConfig config = new FinalPoolConfig();
                config.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai");
                config.setUsername("root");
                config.setPassword("123456");
                finalPool.setFinalPoolConfig(config);
            } else {
                finalPool.setFinalPoolConfig(finalPoolConfig);
            }
            finalPool.init();
        }
        try {
            return finalPool.getDataSource();
        } catch (Exception e) {
            e.printStackTrace();
            new IllegalAccessError(e.getMessage());
        }
        return null;
    }

    public static ResultSet query(String sql, Object... param) {
        ResultSet resultSet = null;
        if (param != null) {
            FinalDataSource dataSource = null;
            try {
                dataSource = getDataSource();
                PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);//获取sql语句
                for (int i = 0; i < param.length; i++) {
                    if (param[i] instanceof String) {
                        ps.setString(i + 1, (String) param[i]);
                    } else if (param[i] instanceof Integer) {
                        ps.setInt(i + 1, (Integer) param[i]);
                    } else if (param[i] instanceof Boolean) {
                        ps.setBoolean(i + 1, (Boolean) param[i]);
                    }
                }
                return ps.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    // 释放数据源
                    finalPool.releaseDataSource(dataSource);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return resultSet;
    }

    public static boolean update(String sql, Object... param) {
        boolean res = false;
        FinalDataSource dataSource = null;
        try {
            dataSource = finalPool.getDataSource();
            PreparedStatement ps = dataSource.getConnection().prepareStatement(sql);//获取sql语句
            for (int i = 0; i < param.length; i++) {
                if (param[i] instanceof String) {
                    ps.setString(i + 1, (String) param[i]);
                } else if (param[i] instanceof Integer) {
                    ps.setInt(i + 1, (Integer) param[i]);
                } else if (param[i] instanceof Date) {
                    ps.setString(i + 1, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(param[i]));
                }
            }
            res = ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放数据源
                finalPool.releaseDataSource(dataSource);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    public static FinalPoolConfig getFinalPoolConfig(){
        return finalPoolConfig;
    }

    // ---------------------------
    public static boolean isEmpty(CharSequence value) {
        return value == null || value.length() == 0;
    }

    public static String getDriver(String url) {
        url = url.toLowerCase();
        if (url.indexOf("mysql") != -1) {
            return "com.mysql.jdbc.Driver";
        }
        return null;
    }


}
